import 'package:flutter/material.dart';

class AnimationPage extends StatefulWidget {
  const AnimationPage({Key? key}) : super(key: key);

  @override
  State<AnimationPage> createState() => _AnimationPageState();
}

class _AnimationPageState extends State<AnimationPage> {
  double _top = 100;
  double _opacity = 0.0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("动画专题"),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          setState(() {
            _top += 20;
            // 状态显示和隐藏
            _opacity = _opacity == 0.0?1.0:0.0;
          });
        },
        child: Text("切换"),
      ),
      //body: AnimatedAlignPage()// AnimatedAlign 动画效果
      //body:AnimatedPositionedPage()
      //body:AnimatedOpacityPage()
      body: SliverAnimatedOpacityPage(),
    );
    
  }
  // // AnimatedAlign 可以用来实现 Widget的对齐排版时形成一个动画过滤
  Widget AnimatedAlignPage(){
    return Container(
        child: AnimatedAlign(
          alignment: Alignment.topCenter,
          // 动画曲线
          curve: Curves.bounceInOut,
          onEnd: (){
            print("执行结束");
          },
          duration: Duration(seconds: 10),
          child: Text(
            "动画",
            style: TextStyle(
            fontWeight:FontWeight.w600,
            color: Colors.blueAccent,
          ),
        ),
        ),
      );
  }
  
  Widget AnimatedPositionedPage(){
    // Positioned 可以给予widget定位  left top right bottom 
    return Container(
      width:double.infinity,
      height: double.infinity,
      child: Stack(
        alignment: Alignment.center,
        children: [
          AnimatedPositioned(
            //动画曲线
            curve: Curves.bounceIn,
            top:_top,
            left:100,
            // 动画时间
            duration:Duration(seconds: 2),
            onEnd: (){
              print("动画执行完成");
            },
            child: Text("动画",style: TextStyle(
              fontWeight: FontWeight.w600,
              color: Colors.blueAccent,
            ),),
            
          )
        ],
      ),
    );
  }
  //AnimatedOpacity 透明度动画
  Widget AnimatedOpacityPage(){
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          AnimatedOpacity(
            opacity: _opacity,
            curve: Curves.fastOutSlowIn,
            duration: Duration(seconds: 2),
            child: Container(
              padding:EdgeInsets.all(20),
              child: FlutterLogo(size: 100),
              color: Colors.grey,
            ),
          )
        ],
      ),
    );
  }
  //SliverAnimatedOpacity 动画解析
  Widget SliverAnimatedOpacityPage(){
    return CustomScrollView(
      slivers: [
        SliverAnimatedOpacity(
          opacity: 1.0, 
          duration: Duration(seconds: 2),
          curve: Curves.fastOutSlowIn,
          sliver: SliverFixedExtentList(
            delegate: SliverChildBuilderDelegate((BuildContext context,int index){
              return Container(
                color: index%2 ==0?Colors.deepOrange[200]:Colors.deepOrange[400],
                child: Text("动画 $index"),

              );
            }),
            itemExtent: 100.0,
          ),
          )
      ],
    );
  }

}